﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace msilgc.cbuilder.CSyntaxTree
{
    internal enum Operator {
        Eq = 0,
        Ne = 1,
        Lt = 2,
        Gt = 3,
        Ge = 4,
        Le = 5,
    }
    internal sealed class ConditionalBranchStatement : ICStatement {
        private readonly Operator _op;
        private readonly string _label;
        private readonly IVariable _var1;
        private readonly IVariable _var2;

        public ConditionalBranchStatement(Operator op, string label, IVariable var1, IVariable var2) {
            _op = op;
            _label = label;
            _var1 = var1;
            _var2 = var2;
        }

        public void Emit(StringBuilder builder)
        {
            builder.Append("if(");
            _var1.Emit(builder);
            switch (_op)
            {
                case Operator.Eq:
                    builder.Append("== ");
                    break;
                case Operator.Ne:
                    builder.Append("!= ");
                    break;
                case Operator.Lt:
                    builder.Append("< ");
                    break;
                case Operator.Gt:
                    builder.Append("> ");
                    break;
                case Operator.Ge:
                    builder.Append(">= ");
                    break;
                case Operator.Le:
                    builder.Append("<= ");
                    break;
            }
            _var2.Emit(builder);
            builder.Append("){ goto ");
            builder.Append(_label);
            builder.AppendLine(" }");
        }
    }
}
